package cn.org.eshow.webapp.action;

import cn.org.eshow.bean.query.BlogQuery;
import cn.org.eshow.common.CommonVar;
import cn.org.eshow.common.page.Page;
import cn.org.eshow.model.Blog;
import cn.org.eshow.service.BlogManager;
import cn.org.eshow.util.PageUtil;
import cn.org.eshow.webapp.util.RenderUtil;
import org.apache.struts2.convention.annotation.AllowedMethods;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;

@Results({@Result(name = "input", location = "add"),
        @Result(name = "list", type = "redirect", location = ""),
        @Result(name = "success", type = "redirect", location = "view/${id}"),
        @Result(name = "redirect", type = "redirect", location = "${redirect}")})
@AllowedMethods({"list", "search",  "delete", "view", "update", "save"})
public class BlogAction extends BaseAction {

    private static final long serialVersionUID = 1L;

    @Autowired
    private BlogManager blogManager;

    private List<Blog> blogs=new ArrayList<Blog>();
    private Blog blog=new Blog();
    private BlogQuery query = new BlogQuery();

    /**
     * @return
     */
    public String list() {
        blogs = blogManager.list(query);
        return LIST;
    }

    /**
     * @return
     */
    public String search() {
        Page<Blog> page = blogManager.search(query);
        blogs = page.getDataList();
        saveRequest("page", PageUtil.conversion(page));
        saveRequest("query", query);
        return LIST;
    }


    /**
     *
     */
    public void delete() {
        blog = blogManager.get(id);
        if (blog != null) {
            blog.setEnabled(Boolean.FALSE);
            blogManager.save(blog);
            saveMessage("删除成功");
            RenderUtil.success("删除成功");
        } else {
            saveMessage("参数不正确");
            RenderUtil.failure("参数不正确");
        }
    }

    /**
     * @return
     */
    public String view() {
        if (id != null) {
            blog = blogManager.get(id);
            blog.setCount(blog.getCount() + CommonVar.STEP);
            blog = blogManager.save(blog);
        } else {
            blog = new Blog();
        }
        return REDIRECT;
    }

    /**
     * @return
     * @throws Exception
     */
    public String update() throws Exception {
        Blog old = blogManager.get(id);
        old = blogManager.updateBlog(old,blog);
        saveMessage("修改成功");
        return REDIRECT;
    }

    /**
     * @return
     * @throws Exception
     */
    public String save() throws Exception {
//        blog.setUser(getSessionUser());
        blog = blogManager.save(blog);
        saveMessage("添加成功");
        return REDIRECT;
    }

    public List<Blog> getBlogs() {
        return blogs;
    }

    public void setBlogs(List<Blog> blogs) {
        this.blogs = blogs;
    }

    public Blog getBlog() {
        return blog;
    }

    public void setBlog(Blog blog) {
        this.blog = blog;
    }

    public BlogQuery getQuery() {
        return query;
    }

    public void setQuery(BlogQuery query) {
        this.query = query;
    }


}